package com.ptu.controller.admin;

import com.ptu.constant.JwtClaimsConstant;
import com.ptu.context.BaseContext;
import com.ptu.dto.StoreDTO;
import com.ptu.dto.StoreLoginDTO;
import com.ptu.dto.StorePageQueryDTO;
import com.ptu.entity.Store;
import com.ptu.properties.JwtProperties;
import com.ptu.result.PageResult;
import com.ptu.result.Result;
import com.ptu.service.StoreService;
import com.ptu.utils.JwtUtil;
import com.ptu.vo.StoreLoginVO;
import com.ptu.vo.StoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/store")
@Slf4j
@Api(tags = "员工相关接口")
public class StoreController {

    @Autowired
    private StoreService storeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param storeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<StoreLoginVO> login(@RequestBody StoreLoginDTO storeLoginDTO) {
        log.info("员工登录：{}", storeLoginDTO);
        Store store = storeService.login(storeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, store.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        StoreLoginVO storeLoginVO = StoreLoginVO.builder()
                .id(store.getId())
                .userName(store.getUsername())
                .name(store.getName())
                .token(token)
                .build();
        log.info("jwt令牌:{}", token);
        log.info("账号：{}", store.getUsername());
        return Result.success(storeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        log.info("这里退出！！！");
        return Result.success();
    }

    /**
     * 新增员工
     * @param storeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody  StoreDTO storeDTO){
        log.info("新增员工：{}", storeDTO);
        System.out.println("当前线程id:" + Thread.currentThread().getId());
        storeService.save(storeDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param storePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(StorePageQueryDTO storePageQueryDTO){
        log.info("员工分页查询，参数为：{}",storePageQueryDTO);
        PageResult pageResult = storeService.pageQuery(storePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用店铺账号")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("启用禁用店铺账号：{},{}",status,id);
        storeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 启用禁用员工账号
     * @param shopstatus
     * @return
     */
    @PostMapping("/shopstatus/{shopstatus}")
    @ApiOperation("设置店铺营业状态")
    public Result shopStatus(@PathVariable Integer shopstatus){
        Long id = BaseContext.getCurrentId();
        log.info("设置店铺营业状态：{},{}",shopstatus,id);
        storeService.shopStatus(shopstatus,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询店铺")
    public Result<StoreVO> getById(@PathVariable Long id){
        StoreVO storeVO = storeService.getById(id);
        return Result.success(storeVO);
    }

    /**
     * 编辑员工信息
     * @param storeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑店铺信息")
    public Result update(@RequestBody StoreDTO storeDTO){
        log.info("编辑店铺信息：{}",storeDTO);
         storeService.update(storeDTO);
        return Result.success();
    }
}
